import axios from "axios";
import { baseUrl } from "../util";
export default {
    state: {
        id: "",
        username: "",
        photo: "",
        token: "",
        is_login: false,
    },
    getters: {

    },
    actions: {
        async login(context, data) {
            console.log("进入user.js");
            try {
                console.log(baseUrl + "account/login");
                console.log(data.username);
                console.log(data.password);
                const resp = await axios({
                    method: 'post',
                    url: baseUrl + "account/login",
                    params: {
                        username: data.username,
                        password: data.password
                    }
                })
                //resp = await.resp;
                console.log(resp);
                if (resp.data.error_message === "success") {
                    console.log("登陆成功");
                    let token = resp.data.token;
                    localStorage.setItem("jwt_token", token);
                    context.commit("updateToken", token);
                    context.commit("updateUser", {
                        id: resp.data.id,
                        username: resp.data.username,
                        photo: resp.data.photo,
                        token: resp.data.token,
                        is_login: true,
                    })
                    return true;
                } else {
                    console.log("登陆失败");
                    return false;
                }
            } catch (e) {
                console.log(e);
                console.log("登陆失败");
            }
        },
        async register(context, data) {
            console.log("进入user.js");
            try {
                console.log(baseUrl + "account/register");
                console.log(data.username);
                console.log(data.password);
                const resp = await axios({
                    method: 'post',
                    url: baseUrl + "account/register",
                    params: {
                        username: data.username,
                        password: data.password,
                        confirmedPassword: data.confirmedPassword,
                    }
                })
                console.log(resp);
                if (resp.data.error_message === "success") {
                    console.log("注册成功");
                    return true;
                } else {
                    console.log("注册失败");
                    return false;
                }
            } catch (e) {
                console.log(e);
                console.log("注册失败");
                return false;
            }
        },
        logout(context) {
            context.commit("updateUser", {
                id: "",
                username: "",
                photo: "",
                token: "",
                is_login: false,
            })
        }
    },
    mutations: {
        updateUser(state, user) {
            state.id = user.id;
            state.username = user.username;
            state.photo = user.photo;
            state.is_login = user.is_login;
            state.token = user.token;
        },
        updateToken(state, token) {
            state.token = token;
        },

    },
    modules: {

    }

}